package com.example.myapplication;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.util.Size;

import androidx.camera.core.Preview;

public class CameraUtils {
    public static Size getMaxResolution(Context context, String cameraId) {
        CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
        try {
            CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
            StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map != null) {
                Size[] outputSizes = map.getOutputSizes(Preview.class);
                if (outputSizes != null && outputSizes.length > 0) {
                    Size maxSize = outputSizes[0];
                    for (Size size : outputSizes) {
                        if (size.getWidth() * size.getHeight() > maxSize.getWidth() * maxSize.getHeight()) {
                            maxSize = size;
                        }
                    }
                    return maxSize;
                }
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
